import Request from 'utils/request';
import SystemUtil from 'utils/systemUtil';

import CircleTheme from 'models/sysCircle/circleTheme';
import CircleZone from 'models/sysCircle/circleZone';
import CircleStat from 'models/sysCircle/circleStat';
import CircleZoneLog from 'models/sysCircle/circleZoneLog';

export default class SysCircleApi {
  static getThemeData(limit) {
    const apiName = 'api/wehcatshop/selfSpaceThemes';

    let postData = {};
    if (!SystemUtil.isEmpty(limit)) {
      postData.limit = limit;
    } else {
      postData.limit = null;
    }

    return Request.post(apiName, postData).then(res => {
      let themes = res.data;
      let themeList = [];
      for (let i = 0; i < themes.length; i++) {
        let theme = themes[i];
        theme = new CircleTheme(theme, i);
        themeList.push(theme);
      }
      return themeList;
    });
  }

  static releaseData(releaseData) {
    const apiName = 'api/wehcatshop/spaceAdd';

    let postData = releaseData.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static getCircleZoneData(pagination, type, option = null, isKeep = false) {
    const apiName = 'api/wehcatshop/spaceList';

    let postData = {};
    if (isKeep) {
      let count = pagination.currentPage * pagination.pageSize;
      postData = {
        limit: count,
        page: 1
      };
    } else {
      postData = {
        limit: pagination.pageSize,
        page: pagination.currentPage
      };
    }
    postData.type = type;

    if (option !== null) {
      let themeTag = option.themeTag;
      if (themeTag !== undefined) {
        postData.themeTag = themeTag;
      }
      let unionid = option.unionid;
      if (unionid !== undefined) {
        postData.unionid = unionid;
      }
    }

    return Request.post(apiName, postData).then(res => {
      let circleZones = res.data;
      let circleZoneList = [];
      for (let i = 0; i < circleZones.length; i++) {
        let circleZone = circleZones[i];
        circleZone = new CircleZone(circleZone);
        circleZoneList.push(circleZone);
      }
      pagination.totalCount = res.rows;
      return circleZoneList;
    });
  }

  static getCircleZoneById(spaceId, hasObject = true) {
    const apiName = 'api/wehcatshop/querySpaceById';

    return Request.post(apiName, {
      spaceId
    }).then(res => {
      let circleZone = res.data;
      if (hasObject) {
        circleZone = new CircleZone(circleZone);
      }
      return circleZone;
    });
  }

  static readCircleZone(ids) {
    const apiName = 'api/wehcatshop/spaceReadCountAdd';
    return Request.post(apiName, {
      spaceId: ids
    }).then(res => {
      return res.msg;
    });
  }

  static getCircleZoneLogById(logId) {
    const apiName = 'api/wehcatshop/querySpaceLogById';

    return Request.post(apiName, {
      manliSpaceLogId: logId
    }).then(res => {
      return res.data;
    });
  }

  static praiseCircleZone(circle) {
    const apiName = 'api/wehcatshop/spaceLikeCountAdd';
    return Request.post(apiName, {
      spaceId: circle.spaceId
    }).then(() => {
      return this.getCircleZoneById(circle.spaceId, false).then(circleZone => {
        return circleZone;
      });
    });
  }

  static getCircleLogData(spaceId, pagination, hasObject = true, isKeep = false) {
    const apiName = 'api/wehcatshop/spaceReviewList';
    let postData;
    if (isKeep) {
      let count = pagination.currentPage * pagination.pageSize;
      postData = {
        limit: count,
        page: 1
      };
    } else {
      postData = {
        limit: pagination.pageSize,
        page: pagination.currentPage
      };
    }
    postData.spaceId = spaceId;

    return Request.post(apiName, postData).then(res => {
      let logList = res.data;
      if (!hasObject) {
        return {
          logList: logList,
          count: res.rows
        };
      }

      let logs = [];
      for (let i = 0; i < logList.length; i++) {
        let log = logList[i];
        log = new CircleZoneLog(log);
        logs.push(log);
      }

      pagination.totalCount = res.rows;
      return logs;
    });
  }

  static praiseCircleZoneLog(log) {
    const apiName = 'api/wehcatshop/spaceReviewLikeCountAdd';
    return Request.post(apiName, {
      manliSpaceLogId: log.logId
    }).then(() => {
      return this.getCircleZoneLogById(log.logId).then(log => {
        return log;
      });
    });
  }

  static sendCircleLog(circleLog) {
    const apiName = 'api/wehcatshop/spaceReviewAdd';
    let postData = circleLog.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static getPersonalStatData(unionid) {
    const apiName = 'api/wehcatshop/selfSpaceStatistics';

    return Request.post(apiName, {
      unionid
    }).then(res => {
      let personalData = res.data;
      personalData = new CircleStat(personalData);
      return personalData;
    });
  }
}
